/**
 * 
 */
package de.cbf.cam.locations.model.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;

import de.cbf.cam.locations.model.interfaces.ILocation;

/**
 * @author Stefan Alter
 *
 */
public class CompositeLocationTree extends Composite {
	private String pathSeparator;  //  @jve:decl-index=0:
	private Tree treeLocations = null;
	private TreeViewer treeViewer = null;	
	/**
	 * @param parent
	 * @param style
	 */
	public CompositeLocationTree(Composite parent, int style) {
		super(parent, style);
		initialize();	
		
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        GridLayout gridLayout = new GridLayout();
        gridLayout.horizontalSpacing = 0;
        gridLayout.marginWidth = 0;
        gridLayout.marginHeight = 0;
        gridLayout.verticalSpacing = 0;
        GridData gridData = new GridData();
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        gridData.grabExcessVerticalSpace = true;
        gridData.verticalAlignment = GridData.FILL;
        treeLocations = new Tree(this, SWT.NONE);
        treeLocations.setLayoutData(gridData);
        treeViewer = new TreeViewer(treeLocations);
        treeViewer.setContentProvider(contentProvider);
        treeViewer.setLabelProvider(labelProvider);
        treeLocations.addSelectionListener(locationSelected);
        this.setLayout(gridLayout);
        this.setSize(new Point(343, 319));
			
	}

	public void setLocations(Iterator<? extends ILocation> iterator){
		treeViewer.setInput(iterator);
	}
	
	public String getSelectedLocationPath() {
		
		return null;
	}
	
	public ILocation getSelectedLocation() {
		ISelection selection = treeViewer.getSelection();
		if (selection instanceof TreeSelection) {
			TreeSelection treeSelection = (TreeSelection) selection;
			Object object = treeSelection.getFirstElement();
			if (object instanceof ILocation) {
				ILocation location = (ILocation) object;
				return location;				
			}
		}
		return null;
	}
	/**
	 * @return the pathSeparator
	 */
	public String getPathSeparator() {
		return pathSeparator;
	}
	/**
	 * @param pathSeparator the pathSeparator to set
	 */
	public void setPathSeparator(String pathSeperator) {
		this.pathSeparator = pathSeperator;
	}
	
	private Collection<ActionListener> locationSelectedListeners = new ArrayList<ActionListener>();
	public boolean registerLocationSelectedListener(ActionListener listener){
		return locationSelectedListeners.add(listener);
	}
	public boolean deRegisterLocationSelectedListener(ActionListener listener){
		return locationSelectedListeners.remove(listener);
	}
	private LocationSelectedAdvancer locationSelected = new LocationSelectedAdvancer();
	private class LocationSelectedAdvancer extends SelectionAdapter{		
		/* (non-Javadoc)
		 * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
		 */
		@Override
		public void widgetSelected(SelectionEvent e) {
			ActionEvent event = new ActionEvent(CompositeLocationTree.this, ActionEvent.ACTION_PERFORMED, "locationSelected");
			for (ActionListener listener : locationSelectedListeners) {
				listener.actionPerformed(event);
			}
		}				
	}
	private LocationLabelProvider labelProvider = new LocationLabelProvider();	
	private class LocationLabelProvider extends LabelProvider{		
		@Override
		public String getText(Object element) {
			if (element instanceof ILocation) {
				ILocation location = (ILocation) element;
				return location.getLocationName();
			}
			return null;
		}		
	}
	private LocationContentProvider contentProvider = new LocationContentProvider();
	private class LocationContentProvider implements ITreeContentProvider{

		/* (non-Javadoc)
		 * @see org.eclipse.jface.viewers.TreeNodeContentProvider#getChildren(java.lang.Object)
		 */
		@Override
		public Object[] getChildren(Object parentElement) {
			if (parentElement instanceof ILocation) {
				ILocation location = (ILocation) parentElement;
				return location.getSubLocations().toArray();
				
			}
			return null;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.viewers.TreeNodeContentProvider#getElements(java.lang.Object)
		 */
		@Override
		public Object[] getElements(Object inputElement) {
			Collection<ILocation> locations = new ArrayList<ILocation>();
			if (inputElement instanceof Iterator) {
				@SuppressWarnings("rawtypes")
				Iterator iterator = (Iterator) inputElement;
				while (iterator.hasNext()) {
					Object object = (Object) iterator.next();
					if (object instanceof ILocation) {
						ILocation location = (ILocation) object;
						locations.add(location);
					}
				}
				return locations.toArray();
			}
			return null;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.viewers.TreeNodeContentProvider#getParent(java.lang.Object)
		 */
		@Override
		public Object getParent(Object element) {
			if (element instanceof ILocation) {
				ILocation location = (ILocation) element;
				return location.getParentLocation();
			}
			return null;
		}

		@Override
		public void dispose() {			
		}

		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}

		@Override
		public boolean hasChildren(Object element) {
			if (element instanceof ILocation) {
				ILocation location = (ILocation) element;
				return ! location.getSubLocations().isEmpty();
			}
			return false;
		}
		
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"
